<template>
  <div class="app-container">
    <h4 class="form-header h4">基本信息</h4>
    <el-form ref="form" :model="form" label-width="80px">
      <el-row>
        <el-col :span="8" :offset="2">
          <el-form-item label="团队名称" prop="deptName">
            <el-input v-model="form.deptName" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="8" :offset="2">
          <el-form-item label="负责人" prop="leader">
            <el-input  v-model="form.leader" disabled />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <h4 class="form-header h4">评定表</h4>
    <el-table v-loading="loading" :row-key="getRowKey" @row-click="clickRow" ref="table" @selection-change="handleSelectionChange" :data="assesses.slice((pageNum-1)*pageSize,pageNum*pageSize)">
      <el-table-column label="序号" type="index" align="center">
        <template slot-scope="scope">
          <span>{{(pageNum - 1) * pageSize + scope.$index + 1}}</span>
        </template>
      </el-table-column>
      <el-table-column type="selection" :reserve-selection="true" width="55"></el-table-column>
      <el-table-column label="评定表编号" align="center" prop="assessId" />
      <el-table-column label="评定表名称" align="center" prop="assessName" />
      <el-table-column label="类别" align="center" prop="cateName" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="pageNum" :limit.sync="pageSize" />

    <el-form label-width="100px">
      <el-form-item style="text-align: center;margin-left:-120px;margin-top:30px;">
        <el-button type="primary" @click="submitForm()">提交</el-button>
        <el-button @click="close()">返回</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>

import {getDeptAuthAssess, updateDeptAuthAssess} from "@/api/system/dept";

export default {
  name: "AuthAssess",
  data() {
    return {
       // 遮罩层
      loading: true,
      // 分页信息
      total: 0,
      pageNum: 1,
      pageSize: 10,
      // 选中角色编号
      roleIds:[],
      // 角色信息
      roles: [],
      // 选中评定表编号
      assessIds:[],
      // 评定表信息
      assesses: [],
      // 用户信息
      form: {}
    };
  },
  created() {
    const deptId = this.$route.params && this.$route.params.deptId;
    console.log(this.$route.params)
    if (deptId) {
      this.loading = true;
      getDeptAuthAssess(deptId).then(response => {
        this.form = response.dept;
        console.log("dept",response.dept)
        this.assesses = response.assesses;
        console.log(this.assesses);
        console.log(response.assesses)
        this.total = this.assesses.length;
        this.$nextTick(() => {
          this.assesses.forEach((row) => {
            if (row.flag) {
              this.$refs.table.toggleRowSelection(row);
            }
          });
        });
        this.loading = false;
      })
    }
  },
  methods: {
    /** 单击选中行数据 */
    clickRow(row) {
      this.$refs.table.toggleRowSelection(row);
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.assessIds = selection.map((item) => item.assessId);
    },
    // 保存选中的数据编号
    getRowKey(row) {
      /** 检查row.id是否有重复的缓存对象 */
      if (!this.checkRepeatObj) {
        this.checkRepeatObj = {}
      }
      if (row) {
        if (row.id) {
          if (this.checkRepeatObj[row.id]) {
            if (!row._secondId) {
              row._secondId = Math.random() + ''
            }
            /** 方便根据key重用元素 */
            return row._secondId
          } else {
            this.checkRepeatObj[row.id] = 1
            return row.id
          }
        }
        if (!row._secondId) {
          row._secondId = Math.random() + ''
        }
        return row._secondId
      } else {
        return Math.random() + ''
      }
    },
    /** 提交按钮 */
    submitForm() {
      const deptId = this.form.deptId;
      const assessIds = this.assessIds.join(",");
      updateDeptAuthAssess({ deptId: deptId, assessIds: assessIds }).then((response) => {
        console.log("response==="+response)
        this.$modal.msgSuccess("授权成功");
        this.close();
      });
    },
    /** 关闭按钮 */
    close() {
      const obj = { path: "/dept" };
      this.$tab.closeOpenPage(obj);
    },
  },
};
</script>
